#include "Title.h"
#include "Password.h"
#include "Menu.h"
#include "Rank.h"



#define BTN_START		0
#define BTN_OPTION		1
#define BTN_EXIT		2
#define BTN_ABOUT		3
#define BTN_RANK		4
#define TITLE_BTN_COUNT	5



void Title::InitScene()
{
	g_CResource.StopMusic();
	m_mapSprObj.clear();
	g_CInputData.m_mapInputObject.clear();

	m_sprBackGround.CreateScript( SPR_TYPE_SCENE );
	m_sprBackGround.AddObject( CREATE_IMAGE_PKG_KEY( 0, 0, 0 ) );

	m_mapSprObj[BTN_START].CreateScript( SPR_TYPE_DEFAULT_CB, 240, 400 );
	m_mapSprObj[BTN_START].AddObject( CREATE_IMAGE_PKG_KEY( 1, 0, 0 ) );
	g_CInputData.AddInputObject( BTN_START, m_mapSprObj[BTN_START].GetScriptRect() );

	m_mapSprObj[BTN_OPTION].CreateScript( SPR_TYPE_DEFAULT_CB, 240, 400 + 60 );
	m_mapSprObj[BTN_OPTION].AddObject( CREATE_IMAGE_PKG_KEY( 1, 0, 1 ) );
	g_CInputData.AddInputObject( BTN_OPTION, m_mapSprObj[BTN_OPTION].GetScriptRect() );

	m_mapSprObj[BTN_RANK].CreateScript( SPR_TYPE_DEFAULT_CB, 240, 400 + 120 );
	m_mapSprObj[BTN_RANK].AddObject( CREATE_IMAGE_PKG_KEY( 1, 0, 4 ) );
	g_CInputData.AddInputObject( BTN_RANK, m_mapSprObj[BTN_RANK].GetScriptRect() );

	m_mapSprObj[BTN_ABOUT].CreateScript( SPR_TYPE_DEFAULT_CB, 240, 400 + 180 );
	m_mapSprObj[BTN_ABOUT].AddObject( CREATE_IMAGE_PKG_KEY( 1, 0, 3 ) );
	g_CInputData.AddInputObject( BTN_ABOUT, m_mapSprObj[BTN_ABOUT].GetScriptRect() );

	m_mapSprObj[BTN_EXIT].CreateScript( SPR_TYPE_DEFAULT_CB, 240, 400 + 240 );
	m_mapSprObj[BTN_EXIT].AddObject( CREATE_IMAGE_PKG_KEY( 1, 0, 2 ) );
	g_CInputData.AddInputObject( BTN_EXIT, m_mapSprObj[BTN_EXIT].GetScriptRect() );

}



void Title::RenderScene()
{
	m_sprBackGround.RenderScript();

	m_mapSprObj[BTN_START].RenderScript();
	m_mapSprObj[BTN_OPTION].RenderScript();
	m_mapSprObj[BTN_RANK].RenderScript();
	m_mapSprObj[BTN_ABOUT].RenderScript();
	m_mapSprObj[BTN_EXIT].RenderScript();

//	g_CDDraw.DDrawText( L"v.20100125" , NULL );

//	RECT rc = { 0, 25, 50, 50 };
//	g_CDDraw.DDrawText( L"By Shadow.Yi" , &rc );

}



void Title::RunLogic()
{
	if( m_bLogicStart )
	{
		for( int index = 0; index != TITLE_BTN_COUNT; ++index )
		{
			if( g_CInputData.m_mapInputObject[index].m_nState == BTN_STATE_DOWN ||
				g_CInputData.m_mapInputObject[index].m_nState == BTN_STATE_HOLD )
			{
				if( m_mapSprObj[index].GetCurActionID() != BtnDown )
					g_CResource.PlaySound( SOUND_OVER );

				m_mapSprObj[index].SetAction( BtnDown );

			}
			else if( g_CInputData.m_mapInputObject[index].m_nState == BTN_STATE_ON )
				m_mapSprObj[index].SetAction( BtnMouseOn );
			else if( g_CInputData.m_mapInputObject[index].m_nState == BTN_STATE_UP )
				g_CResource.PlaySound( SOUND_PUSH );
			else
				m_mapSprObj[index].SetAction( NoAction );

		}

		if( g_CInputData.m_mapInputObject[BTN_START].m_nState == BTN_STATE_UP )
		{
			*m_ppScene = new Menu( m_ppScene );

			((Menu*)*m_ppScene)->AddImgOption( MENU_BTN_STORY_MODE, MENU_BTN_TYPE_IMG_BTN );

			if( g_CGame.GetSetting()->dwPassword3 == g_CGame.CalPassword( 3 ) )
				((Menu*)*m_ppScene)->AddImgOption( MENU_BTN_ENDLESS_MODE, MENU_BTN_TYPE_IMG_BTN );
			if( g_CGame.GetSetting()->dwPassword4 == g_CGame.CalPassword( 4 ) )
				((Menu*)*m_ppScene)->AddImgOption( MENU_BTN_BOSS_MODE, MENU_BTN_TYPE_IMG_BTN );

	//		((Menu*)*m_ppScene)->AddImgOption( MENU_BTN_HELP, MENU_BTN_TYPE_IMG_BTN );

			(*m_ppScene)->InitScene();
			return ;

		}
		else if( g_CInputData.m_mapInputObject[BTN_OPTION].m_nState == BTN_STATE_UP )
		{
			*m_ppScene = new Menu( m_ppScene );
			((Menu*)*m_ppScene)->AddImgOption( MENU_BTN_SOUND, g_CGame.m_Setting.bSound ? MENU_BTN_TYPE_IMG_ON : MENU_BTN_TYPE_IMG_OFF );
			((Menu*)*m_ppScene)->AddImgOption( MENU_BTN_VIBRATE, g_CGame.m_Setting.bVibrate ? MENU_BTN_TYPE_IMG_ON : MENU_BTN_TYPE_IMG_OFF );
			(*m_ppScene)->InitScene();
			return ;

		}
		else if( g_CInputData.m_mapInputObject[BTN_EXIT].m_nState == BTN_STATE_UP )
			SetAnimate( NO_ANIME, ANIME_CMD_EXIT_APP );
		else if( g_CInputData.m_mapInputObject[BTN_ABOUT].m_nState == BTN_STATE_UP )
		{
			*m_ppScene = new Menu( m_ppScene );
			((Menu*)*m_ppScene)->AddAbout();
			(*m_ppScene)->InitScene();
			return ;

		}
		else if( g_CInputData.m_mapInputObject[BTN_RANK].m_nState == BTN_STATE_UP )
		{
			*m_ppScene = new Rank( m_ppScene );
			(*m_ppScene)->InitScene();
			((Rank*)*m_ppScene)->SetScoreRank();
			return ;

		}

	}

}



Title::Title( CScene** ppScene )
{
	m_ppScene = ppScene;
	m_bLogicStart = false;

}